package com.redsoft.spirit.dao;

import java.util.List;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import com.redsoft.spirit.entity.Account;

/**
 * 后台用户管理DAO
 * 
 * <pre>
 * 后台用户管理DAO
 * </pre>
 * 
 * @author 杨雷
 * @since 1.0
 */
public interface AccountRepository extends BaseRepository<Account, String> {

	Page<Account> findAll(Pageable pageable);

	/**
	 * 获取所有用户
	 * 
	 * <pre>
	 * 获取所有用户
	 * </pre>
	 * 
	 * @return
	 */
	@Query("from Account a where a.isActive = '1'")
	List<Account> findAll();

	@Query("from Account a where a.isActive = '1' and a.extA is not null ")
	List<Account> findAllWithMobileType();

	@Query("from Account a where a.isActive = '1' and a.logonMode = ?1 ")
	List<Account> findAllByLogonMode(int logonMode);

	/**
	 * 根据ID获取一条用户
	 * 
	 * @param id
	 * @return
	 */
	Account getById(String id);

	@Modifying
	@Query("delete from Account a where a.id in ?1")
	void deleteUsers(List<String> userIds);

	@Query("select a from Account a where a.id not in(select ra.accountId from RoleAccount ra where ra.roleId = ?1) and a.realName like %?2% and a.logonMode = 1 and a.id in (select de.empId from DeptEmp de where de.deptId = ?3) ")
	List<Account> getUserListByOutRoleWithDept(String roleId, String searchKey, String deptId);

	@Query("select a from Account a where a.id not in(select ra.accountId from RoleAccount ra where ra.roleId = ?1) and a.realName like %?2% and a.logonMode = 1 ")
	List<Account> getUserListByOutRole(String roleId, String searchKey);

	@Query("select a from Account a where a.id in(select ra.accountId from RoleAccount ra where ra.roleId = ?1)")
	List<Account> getUserListByInRole(String roleId);

	/**
	 * 根据用户名获取一个用户
	 * 
	 * @param userName
	 * @return
	 */
	Account getByAccountName(String userName);

	@Modifying
	@Query("update Account set extB = '' where extB = ?1 ")
	void updateDeviceToken(String deviceToken);
}
